package com.avic.system.common.convertor;

import com.avic.system.controller.info.ChildrenMenuPermissionInfo;
import com.avic.system.controller.info.ChildrenPermissionInfo;
import com.avic.system.controller.info.MenuPermissionInfo;
import com.avic.system.dal.model.SystemPermission;
import com.avic.system.dal.model.vo.MenuPermission;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 权限联合菜单转换器
 *
 * @author: huangsheng
 * @date: 2017/10/24
 */
public class MenuPermissionConvertor {

    public static List<MenuPermissionInfo> menuAndPermissionListToMenuPermissionInfo(List<MenuPermission> menuAndPermissions) {
        if (null != menuAndPermissions && menuAndPermissions.size() > 0) {
            List<MenuPermissionInfo> menuAndPermissionInfos = new ArrayList<>(menuAndPermissions.size());
            menuAndPermissions.forEach(menuAndPermission -> {
                // 转换一级菜单
                menuAndPermissionInfos.add(MenuPermissionConvertor.menuAndPermissionToMenuPermissionInfo(menuAndPermission));
            });
            return menuAndPermissionInfos;
        }
        return Collections.emptyList();
    }

    public static MenuPermissionInfo menuAndPermissionToMenuPermissionInfo(MenuPermission menuAndPermission) {
        if (null == menuAndPermission) {
            return null;
        }
        MenuPermissionInfo menuAndPermissionInfo = new MenuPermissionInfo();
        menuAndPermissionInfo.setTreeId(MenuPermissionInfo.EMPTY_TREE_ID);
        menuAndPermissionInfo.setLabel(menuAndPermission.getMenuName());
        // 转换二级菜单
        menuAndPermissionInfo.setChildren(MenuPermissionConvertor
                .menuPermissionListToChildrenMenuPermissionInfo(menuAndPermission.getSystemPermissions(), menuAndPermission.getSubMenus()));
        return menuAndPermissionInfo;
    }

    public static List<ChildrenMenuPermissionInfo> menuPermissionListToChildrenMenuPermissionInfo(
            List<SystemPermission> systemPermissions, List<MenuPermission> menuPermissions) {
        List<ChildrenMenuPermissionInfo> subMenuPermissionInfos = new ArrayList<>(menuPermissions.size());
        if (null != menuPermissions && menuPermissions.size() > 0) {

            menuPermissions.forEach(menuPermission -> {
                ChildrenMenuPermissionInfo subMenuPermissionInfo = new ChildrenMenuPermissionInfo();
                subMenuPermissionInfo.setTreeId(MenuPermissionInfo.EMPTY_TREE_ID);
                subMenuPermissionInfo.setLabel(menuPermission.getMenuName());
                subMenuPermissionInfo.setChildren(MenuPermissionConvertor.systemPermissionListToChildrenPermissionInfo(menuPermission.getSystemPermissions()));
                subMenuPermissionInfos.add(subMenuPermissionInfo);
            });
        } else if (null != systemPermissions && systemPermissions.size() > 0) {
            systemPermissions.forEach(systemPermission -> {
                ChildrenMenuPermissionInfo subMenuPermissionInfo = new ChildrenMenuPermissionInfo();
                subMenuPermissionInfo.setTreeId(systemPermission.getId());
                subMenuPermissionInfo.setLabel(systemPermission.getPermissionName());
                subMenuPermissionInfo.setChildren(new ArrayList<>());
                subMenuPermissionInfos.add(subMenuPermissionInfo);
            });
        }
        return subMenuPermissionInfos;
    }

    public static List<ChildrenPermissionInfo> systemPermissionListToChildrenPermissionInfo(List<SystemPermission> systemPermissions) {
        List<ChildrenPermissionInfo> childrenPermissionInfos = new ArrayList<>();
        if (null != systemPermissions && systemPermissions.size() > 0) {
            systemPermissions.forEach(systemPermission -> {
                ChildrenPermissionInfo childrenPermissionInfo = new ChildrenPermissionInfo();
                childrenPermissionInfo.setLabel(systemPermission.getPermissionName());
                childrenPermissionInfo.setTreeId(systemPermission.getId());
                childrenPermissionInfos.add(childrenPermissionInfo);
            });
        }
        return childrenPermissionInfos;
    }
}
